
package com.badlogic.androidgames.asyncutil;


import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;


import com.badlogic.androidgames.framework.impl.GLGame;

import android.app.ProgressDialog;
import android.os.AsyncTask;

public class AsyncJSONBroker extends AsyncTask<JSONReq, Integer, JSONObject> 
{
	GLGame g;
	ProgressDialog progressBar;
	
	public AsyncJSONBroker(GLGame g)
	{
		super();
		this.g = g;
	}

	/*protected JSONObject doInBackground(JSONReq... req) {
		publishProgress(1);
		String response;
		JSONObject jsonResponse;
		try {
			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost httpost = new HttpPost(req[0].path);
			Iterator iter = req[0].params.entrySet().iterator();

			//JSONObject holder = new JSONObject();
			JSONObject data= new JSONObject();
			while (iter.hasNext()) {


				//Map.Entry pairs = (Map.Entry) iter.next();
				//String key = (String) pairs.getKey();
				//Map m = (Map) pairs.getValue();

				//JSONObject data = new JSONObject();
				//Iterator iter2 = m.entrySet().iterator();
				//while (iter2.hasNext()) {
				//	Map.Entry pairs2 = (Map.Entry) iter2.next();
				//	data.put((String) pairs2.getKey(),
				//			(String) pairs2.getValue());


				Map.Entry pairs = (Map.Entry) iter.next();
				String key = (String) pairs.getKey();
				String value = (String) pairs.getValue();
				//Map m = (Map) pairs.getValue();

				//JSONObject data = new JSONObject();
				//Iterator iter2 = m.entrySet().iterator();
				//while (iter2.hasNext()) {
				//Map.Entry pairs2 = (Map.Entry) iter2.next();
				data.put(key,value);
			}
			//holder.put(key, data);
			//}

			String deb = data.toString();
			StringEntity se = new StringEntity(data.toString());
			httpost.setEntity(se);
			//attenzione allo slash aggiunto da togliere!!!!!!!!!!!!!!!!!!!!!!!!
			httpost.setHeader("Accept", "*//*");
			httpost.setHeader("Content-type", "application/json");

			ResponseHandler responseHandler = new BasicResponseHandler();
			response = httpclient.execute(httpost, responseHandler);
			jsonResponse = new JSONObject(response);

		} catch (Exception e) {
			e.printStackTrace();
			jsonResponse = null;	
		}
		return jsonResponse;	
	}*/
	
	
	protected JSONObject doInBackground(JSONReq... req) 
	{
		publishProgress(1);
		String response;
		JSONObject jsonResponse;
		try 
		{
			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpPost httpost = new HttpPost(req[0].path);
			Iterator iter = req[0].params.entrySet().iterator();

			JSONObject data= new JSONObject();
			while (iter.hasNext()) 
			{
				Map.Entry pairs = (Map.Entry) iter.next();
				String key = (String) pairs.getKey();
				String value = (String) pairs.getValue();
				data.put(key,value);
			}

			String deb = data.toString();
			StringEntity se = new StringEntity(data.toString());
			httpost.setEntity(se);
			httpost.setHeader("Accept", "*/*");
			httpost.setHeader("Content-type", "application/json");

			ResponseHandler responseHandler = new BasicResponseHandler();
			response = httpclient.execute(httpost, responseHandler);
			jsonResponse = new JSONObject(response);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			jsonResponse = null;	
		}
		return jsonResponse;	
	}
	
	
	protected void onProgressUpdate(Integer... progress) 
	{
		//progressBar = new ProgressDialog(g);
		//progressBar.setMessage("Communication ...");
		//progressBar.show();
	}

	protected void onPostExecute(JSONObject res) 
	{
		//progressBar.hide();
		//progressBar.dismiss();
	}
}